package org.jeecg.modules.demo.storeinfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.demo.storeinfo.entity.StoreInfo;
import org.jeecg.modules.demo.storeinfo.mapper.StoreInfoMapper;
import org.jeecg.modules.demo.storeinfo.service.IStoreInfoService;
import org.jeecg.modules.util.ToolsUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 店铺信息
 * @Author: jeecg-boot
 * @Date: 2022-01-20
 * @Version: V1.0
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Resource
    StoreInfoMapper storeInfoMapper;

    @Override
    public int isStoreByUserId(String userId) {
        Integer count = storeInfoMapper.isStoreByUserId(userId);
        return count == null ? 0 : count;
    }

    @Override
    public String getStoreExpireTip(String id) {
        Map<String, Object> map = storeInfoMapper.getStoreExpireTipById(id);
        if (map == null) {
            return "";
        }
        return getTipsByMap(map);
    }

    @Override
    public Map<String, Object> getStoreExpireTips(String userId) {
        List<Map<String, Object>> listMap = storeInfoMapper.getStoreExpireTips(userId);
        StringBuffer bufNear = new StringBuffer();
        StringBuffer bufExpire = new StringBuffer();
        listMap.forEach(m -> {
            String tipNear = getTipsByMap(m);
            if (StringUtils.isNotEmpty(tipNear)) {
                if (bufNear.length() > 0) bufNear.append(",");
                bufNear.append(tipNear);
            }
            String tipExpire = ToolsUtils.getString(m.get("content"));
            if (StringUtils.isNotEmpty(tipExpire)) {
                if (bufExpire.length() > 0) bufExpire.append(",");
                bufExpire.append(tipExpire);
            }
        });

        Map<String, Object> map = new HashMap<>();
        map.put("nearTip", bufNear);
        map.put("expireTip", bufExpire);
        return map;
    }

    @Override
    public StoreInfo getStoreByXcxAppid(String xcxAppId) {
        return storeInfoMapper.getStoreByXcxAppid(xcxAppId);
    }

    private String getTipsByMap(Map<String, Object> map) {
        String content15 = ToolsUtils.getString(map.get("content15"));
        if (StringUtils.isNotEmpty(content15)) {
            return content15;
        }
        String content7 = ToolsUtils.getString(map.get("content7"));
        if (StringUtils.isNotEmpty(content7)) {
            return content7;
        }
        String content6 = ToolsUtils.getString(map.get("content6"));
        if (StringUtils.isNotEmpty(content6)) {
            return content6;
        }
        String content5 = ToolsUtils.getString(map.get("content5"));
        if (StringUtils.isNotEmpty(content5)) {
            return content5;
        }
        String content4 = ToolsUtils.getString(map.get("content4"));
        if (StringUtils.isNotEmpty(content4)) {
            return content4;
        }
        String content3 = ToolsUtils.getString(map.get("content3"));
        if (StringUtils.isNotEmpty(content3)) {
            return content3;
        }
        String content2 = ToolsUtils.getString(map.get("content2"));
        if (StringUtils.isNotEmpty(content2)) {
            return content2;
        }
        String content1 = ToolsUtils.getString(map.get("content1"));
        if (StringUtils.isNotEmpty(content1)) {
            return content1;
        }

        return "";
    }

    public String getBossGzhOpenId(String id) {
        return storeInfoMapper.getBossGzhOpenId(id);
    }

}
